import java.awt.EventQueue;

import javax.swing.UIManager;

import lib.logging.Level;
import lib.logging.Logger;
import settings.SettingsControl;

public class App {

	private final static SettingsControl prefctrl = new SettingsControl();

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// Default log level for the program
		Logger.instance.setLogLevel(Level.INFO);

		updateBierDopjeMapping();

		CommandLine cmd = new CommandLine(prefctrl);
		try {
			cmd.Parse(args);
		} catch (Exception e) {
			Logger.instance.error(e.getMessage());
		}
		if (cmd.isGui()) {
			EventQueue.invokeLater(new Runnable() {
				public void run() {
					try {
						UIManager.setLookAndFeel(UIManager
								.getSystemLookAndFeelClassName());
						MainWindow window = new MainWindow(prefctrl);
						window.setVisible(true);
					} catch (Exception e) {
						Logger.instance.error(Logger.stack2String(e));
					}
				}
			});
		} else {
			cmd.CheckUpdate();
			cmd.executeArgs();
		}
	}

	private static void updateBierDopjeMapping() {
		try {
			if (prefctrl.getSettings().isAutoUpdateMapping()) {
				Logger.instance.log("Auto updating mapping ....");
				prefctrl.updateMappingFromOnline();
			}
		} catch (Throwable e) {
			Logger.instance.error(Logger.stack2String(e));
		}
	}
}
